package com.tyx.www.mynotepad;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.HashMap;

import dialog.CustomDialog;

public class MainActivity extends Activity {
    private EditText main_user_edit,main_password_edit;
    private Button main_enter_btn,main_enroll_btn;
    private Dialog dialog;
    private String mainName,mainPassword;
    private String getMainName,getMainPassword;
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findView();
        initView();

    }



    private void findView() {
        main_user_edit= (EditText) findViewById(R.id.main_user_edit);
        main_password_edit= (EditText) findViewById(R.id.main_password_edit);
        main_enter_btn = (Button) findViewById(R.id.main_enter);
        main_enroll_btn = (Button) findViewById(R.id.main_enroll);
    }

    private void initView() {
        main_enter_btn.setOnClickListener(onClickListener);
        main_enroll_btn.setOnClickListener(onClickListener);
        initdialog();
    }
    private void initdialog(){
        dialog=new CustomDialog(MainActivity.this,R.layout.item_dailog_fail_item);
        dialog.findViewById(R.id.dailog_fail).setOnClickListener(onClickListener);
    }
    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.main_enter:
                    mainName=main_user_edit.getText().toString();
                    mainPassword=main_password_edit.getText().toString();
                    sharedPreferences=getSharedPreferences(mainName,MODE_PRIVATE);
                    getMainName=sharedPreferences.getString("user_name","null");
                    getMainPassword=sharedPreferences.getString("user_password","null");
                    if (getMainName.equals(mainName) && getMainPassword.equals(mainPassword)){
                        Intent intent=new Intent(MainActivity.this,EditActivity.class);
                        intent.putExtra("Name",getMainName);
                        startActivity(intent);
                    }else{
                        dialog.show();
                    }
                    break;
                case R.id.main_enroll:
                    startActivity(new Intent(MainActivity.this,EnrollActivity.class));
                    break;
                case R.id.dailog_fail:
                    dialog.dismiss();
                    break;
            }
        }
    };

}
